package com.utils.push;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 极光推送工具类
 */
public class JPush {

    private static final String APP_KEY = "d6636a5bb61de0c75e607b7b";

    private static final String MASTER_SECRET = "1f8ce5a6315fb46522e7def9";

    public static void sendPush(String title, String content, Map<String, String> map, Integer type) {
        JPushClient jPushClient = null;
        try {
            map.put("timestamp", "" + System.currentTimeMillis());
            map.put("uuid", "" + UUID.randomUUID().toString().replace("-", ""));
            jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
            if (type == 1 || type == 3) {
                jPushClient.sendPush(buildIos(title, content, map));
            }
            if (type == 2 || type == 3) {
                jPushClient.sendPush(buildAndroid(title, content, map));
            }
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != jPushClient) {
                jPushClient.close();
            }
        }
    }

    /*private static PushPayload buildIos(String content, Map map) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.ios(content, map))
                .build();
    }*/

    private static PushPayload buildAndroid(String title, String content, Map map) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.android(content, title, map))
                .build();
    }

    private static PushPayload buildIos(String title, String content, Map map) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(
                        Notification.newBuilder().addPlatformNotification(
                                IosNotification.newBuilder()
                                        .incrBadge(1)
                                        .addExtras(map)
                                        .setSound("sound.caf")
                                        .setAlert(IosAlert.newBuilder().setTitleAndBody(title, null, content)
                                                .build())
                                        .build())
                                .build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build())
                .build();
    }

    public static PushPayload buildAlias(String telephone, String title, String content, Map map) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(telephone))
                .setNotification(
                        Notification.newBuilder()
                                .addPlatformNotification(AndroidNotification.newBuilder()
                                        .setTitle(title)
                                        .setAlert(content)
                                        .addExtras(map)
                                        .build())
                                .addPlatformNotification(IosNotification.newBuilder()
                                        .incrBadge(1)
                                        .addExtras(map)
                                        .setSound("sound.caf")
                                        .setAlert(IosAlert.newBuilder().setTitleAndBody(title, null, content)
                                                .build())
                                        .build())
                                .build())
                .build();
    }

    public static void main(String[] args) {
        try {
            JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
            Map<String, String> map = new HashMap<>();
            map.put("id", "111");
            PushResult result = jPushClient.sendPush(buildAlias("18637328744", "好芳法课堂", "福利来啦！“好芳法课堂“全场领券满减活动已经开始了！满800减80、满1200减150、满2000减300，优惠券仅有5000份，领完即止!", map));
            System.out.println(new Gson().toJson(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
